package net.oschina.app.adapter;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import net.oschina.app.base.BaseFragment;
import net.oschina.app.fragment.me.noticefragment.Fans1Fragment;
import net.oschina.app.fragment.me.noticefragment.Notice1Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Vashon on 2017/1/13.
 */
public class FansAdapter extends FragmentPagerAdapter{
    private String[] tabNames;
    private List<BaseFragment> datas = new ArrayList<>();

    //构造方法，传递片段管理器
    public FansAdapter(FragmentManager fm) {
        super(fm);
        tabNames = new String[]{"关注", "粉丝"};
    }

    // 设置数据的方法
    public void setDatas(List<BaseFragment> datas) {
        this.datas = datas;
        notifyDataSetChanged();//发数据改变的通知
    }

    // 取得当前要展示的片段
    @Override
    public BaseFragment getItem(int position) {
        return getFragment(position);
    }

    // 用于缓存片段的集合 HashMap  ： Integer(key)-->postion   BaseFragment(value)
    private static HashMap<Integer,BaseFragment> fragments=new HashMap<>();
    private BaseFragment getFragment(int position) {
        BaseFragment fragment=fragments.get(position);
        // 当fragmnet为空，则依据产品型号创建产品片段
        if(fragment==null){
            switch (position){
                case 0:
                    fragment=new Notice1Fragment();
                    break;
                case 1:
                    fragment=new Fans1Fragment();
                    break;
                default:
                    fragment=new Notice1Fragment();
            }
            //把片段添加到集合中
            fragments.put(position,fragment);
        }
        return  fragment;
    }

    //要展示的片段的数量
    @Override
    public int getCount() {
        return tabNames.length;
    }

    //返回页面标题 ，PagerSlidingTabStrip的标签要依赖该方法
    @Override
    public CharSequence getPageTitle(int position) {
        return tabNames[position];
    }
}
